package com.zdf.license.creator.service;

import com.zdf.license.core.model.LicenseExtraParam;
import com.zdf.license.core.utils.Base64Utils;
import com.zdf.license.core.utils.DateUtils;
import com.zdf.license.creator.model.LicenseCreatorManager;
import com.zdf.license.creator.model.LicenseCreatorParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 证书生成接口实现
 */
@Slf4j
public class LicenseCreatorService {

    /**
     * 解密证书申请码
     * @param code 证书申请码
     * @return 证书信息
     */
    public LicenseExtraParam decodeCode(String code){
        return Base64Utils.decodeLicenseExtraParam(code);
    }

    /**
     * 生成证书
     * @param param 证书创建需要的参数对象
     *              LicenseCreatorProperties.buildLicenseCreatorParam()
     *              这里可以使用默认参数创建
     */
    public byte[] generateLicense(LicenseCreatorParam param) {
        LicenseCreatorManager licenseCreator = new LicenseCreatorManager(param);
        byte[] licenseInfo = licenseCreator.create();
        if (licenseInfo == null) {
            throw new RuntimeException("生成证书失败！");
        }
        log.info("证书生成成功，证书有效期：" +DateUtils.date2Str(param.getIssuedTime())+ " - " + DateUtils.date2Str(param.getExpiryTime()));
        return licenseInfo;
    }
}
